package com.sq.partner.manager.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.dao.AdjSoaResultDao;
import com.sq.partner.manager.mybatis.model.AdjSoaResult;
import com.sq.partner.manager.mybatis.model.DictDto;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

@Controller
@RequestMapping("/recCheckController")
public class RecCheckController {

	@Resource
	AdjSoaResultDao adjSoaResultDao;
	
	@RequestMapping("/queryRecCheckList")
	@ResponseBody
	public Page<AdjSoaResult> queryRecCheckList(int current,int size,String checkDate,String checkType,String isExc) throws Exception{
		return adjSoaResultDao.queryRecCheckList(current, size, checkDate, checkType,isExc);
	}
	
	@RequestMapping(value = "/adjSoaResult", method = RequestMethod.GET)
	public String toListPage(){
		return "/mayiAdj/soaCheck";
	}


	@RequestMapping(value = "exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletResponse response, HttpServletRequest request, Integer current,Integer size,String checkDate,String checkType,String isExc) throws Exception {
		checkDate = checkDate == null ? "" : checkDate;
		List<AdjSoaResult> adjSoaResultList = adjSoaResultDao.queryRecCheckList(current, size, checkDate, checkType,isExc).getData();
		String fileName = "蚂蚁对账信息表";
		String sheetName = "蚂蚁对账信息";
		// 列数
		int columnNumber = 10;
		// 列宽
		int[] columnWidth = {5, 25, 20, 10, 10, 15, 10, 15, 15, 25};
		// 列名
		String[] columnName = {"序号", "批次日期", "类型", "蚂蚁订单数", "蚂蚁总保费", "蚂蚁总保额", "中华总保费", "中华总保额", "对账结果", "失败原因"};
		// 序号
		int count = 1;
		List<String[]> tempList = new ArrayList<String[]>();
		for (AdjSoaResult a : adjSoaResultList) {
			tempList.add(new String[]{
					StrUtil.valueOf(count++),a.getBatchDate(),a.getType(),StrUtil.valueOf(a.getInsureNumber()),StrUtil.valueOf(a.getMayiAmt()),
					StrUtil.valueOf(a.getMayiPrem()),StrUtil.valueOf(a.getCicPrem()),StrUtil.valueOf(a.getCicAmt()),a.getSign(),a.getFailCause()
			});
		}
		String[][] haidaInfos = tempList.toArray(new String[][]{});
		HSSFWorkbook wb = generateHssWorkBook(sheetName, columnNumber, columnWidth, columnName, haidaInfos);
		response.setContentType("application/ms-excel;charset=utf-8");
		String userAgent = request.getHeader("user-agent").toLowerCase();
		if (userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge")) {
			// IE浏览器
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} else {
			// 非IE浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
		}
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		OutputStream os = response.getOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			os.close();
		}
	}

	public HSSFWorkbook generateHssWorkBook(String sheetName, int columnNumber, int[] columnWidth,
											String[] columnName, String[][] haidaInfos) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(sheetName);
		for (int i = 0; i < columnNumber; i++) {
			sheet.setColumnWidth(i, columnWidth[i] * 256);
		}
		HSSFRow headerRow = sheet.createRow(0);
		// 第"0"行行高设置为40
		headerRow.setHeightInPoints(40);
		// 表头单元格样式
		HSSFCellStyle headerStyle = wb.createCellStyle();
		// 设置自动换行
		headerStyle.setWrapText(true);
		// 居中样式
		headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		headerStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
		headerStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
		headerStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
		headerStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
		// 设置表头字体样式
		HSSFFont headerFont = wb.createFont();
		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		headerFont.setFontName("宋体");
		// 设置字体大小
		headerFont.setFontHeightInPoints((short) 10);
		headerStyle.setFont(headerFont);

		HSSFCell cell = null;
		for (int i = 0; i < columnNumber; i++) {
			cell = headerRow.createCell(i);
			cell.setCellValue(columnName[i]);
			cell.setCellStyle(headerStyle);
		}
		HSSFRow row = null;
		// 数据的样式
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		style.setBottomBorderColor(HSSFColor.BLACK.index);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		style.setFont(font);
		for (int i = 0; i < haidaInfos.length; i++) {
			row = sheet.createRow(i + 1);
			for (int j = 0; j < columnNumber; j++) {
				cell = row.createCell(j);
				row.setHeightInPoints(16);
				cell.setCellValue(haidaInfos[i][j]);
				cell.setCellStyle(style);
			}
		}
		return wb;
	}
	/*
	public Page<DictDto> queryDictList(DictDto dictDto,Page<DictDto> page,ModelMap map1) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		if( dictDto!=null){
			if(StrUtil.isNotEmpty(dictDto.getCodeName())){
				map.put("codeName", "%"+dictDto.getCodeName()+"%");
			}
			if(StrUtil.isNotEmpty(dictDto.getCodeDesc())){
				map.put("codeDesc", "%"+dictDto.getCodeDesc()+"%");
			}
		}
		map.put("BEGINROW", (page.getCurrent()-1) * 10 );
		map.put("ENDROW", page.getCurrent() * 10);
		page.setMap(map);
		page=dictManagerService.queryDictDtoPageByCriteria(page);
		
		return page;
	}
	*/
}
